﻿package dk.gaming.rummikub.uicomponents
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import dk.gaming.rummikub.Utils.Utility;
	import dk.gaming.rummikub.backend.Proxy;
	import dk.gaming.rummikub.appcontext.AppContext;
	import dk.gaming.rummikub.events.ProxyEvent;
	import dk.gaming.rummikub.events.GameEvent;
	
	public class Login extends MovieClip
	{
		
		private var UserName : TextField;
		private var Password : TextField;
		private var LoginButton : MovieClip;
		private var proxy : Proxy;
		
		
		public function Login():void
		{
			
			UserName = usernametf;
			Password = passwordtf;
			LoginButton = loginbtn;
			
			proxy = new Proxy();
			LoginButton.addEventListener(MouseEvent.CLICK, ValidateCredentials);
			
		}
		
		private function ValidateCredentials(e:MouseEvent):void
		{
			if(Utility.ValidateEmail(UserName.text) && Utility.ValidatePassword(Password.text))
			{
				proxy.addEventListener(ProxyEvent.LOGIN_REQUEST_RESULT, LoginResult);
				proxy.addEventListener(ProxyEvent.PROXY_ERROR, ProxyError);
				proxy.RequestLogin(UserName.text, Password.text);
			}
			else
			{
				// TODO : popup dialogue to check credentials
			}
		}
		
		private function LoginResult(e:ProxyEvent):void
		{
			AppContext.userID = e.
			this.dispatchEvent(new GameEvent(GameEvent.LOGIN_SUCCESFUL, false, false, "OK", String));
		}
		
		private function ProxyError(e:ProxyEvent):void
		{
			this.dispatchEvent(new ProxyEvent(ProxyEvent.PROXY_ERROR));
		}
		
		
		
		
		
		
	}
	
	
	
}